﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using WXYW.DAL;
using WXYW.Entities;

namespace WXYW.BLL
{
    public class TaskService : RepositoryBase
    {
        public void InsertTask(Task task)
        {
            InsertEntity(task);
        }

        public IQueryable<Task> GetAllOverDueDTask()
        {
            IQueryable<Task> tasks = GetEntities<Task>(t => t.CompleteDate.HasValue && (t.CompleteDate > t.ShouldCompleteDate));
            foreach (Task task in tasks)
            {
                if (!task.UserReference.IsLoaded)
                {
                    task.UserReference.Load();
                }
            }
            return tasks;
        }

        public IQueryable<Task> GetTasksByYearMonthService(int year, int month, int servicestation)
        {
            IQueryable<Task> tasks = GetEntities<Task>(t => t.CreateDate.Value.Year == year && t.CreateDate.Value.Month == month && t.User.ServiceStation.ID == servicestation);
            foreach (Task task in tasks)
            {
                if (!task.UserReference.IsLoaded)
                {
                    task.UserReference.Load();
                }
                if (!task.User1Reference.IsLoaded)
                {
                    task.User1Reference.Load();
                }
            }

            return tasks;
        }

        public IQueryable<Task> GetTasksByYearMonthService(int startyear, int startmonth, int endyear, int endmonth,
            int servicestation, string keyword)
        {
            DateTime startDate = new DateTime(startyear, startmonth, 1);
            startDate = startDate.AddDays(-1);
            DateTime endDate = new DateTime(endyear, endmonth, 1);
            endDate = endDate.AddMonths(1);
            IQueryable<Task> tasks = GetEntities<Task>(t => t.CreateDate > startDate && t.CreateDate < endDate).OrderByDescending(t=>t.CreateDate);
            if (servicestation > 0)
            {
                tasks = tasks.Where(t => t.User.ServiceStation.ID == servicestation);
            }
            if (!string.IsNullOrEmpty(keyword))
            {
                tasks = tasks.Where(t=>t.Content.Contains(keyword) || t.Subject.Contains(keyword) ||
                    t.User.ContactPerson.Contains(keyword) || t.User.CompanyName.Contains(keyword) || t.User.UserName.Contains(keyword) ||
                    t.User1.ContactPerson.Contains(keyword) || t.User1.CompanyName.Contains(keyword) || t.User1.UserName.Contains(keyword));
            }
            foreach (Task task in tasks)
            {
                if (!task.UserReference.IsLoaded)
                {
                    task.UserReference.Load();
                }
                if (!task.User1Reference.IsLoaded)
                {
                    task.User1Reference.Load();
                }
            }

            return tasks;
        }

        public IQueryable<Task> GetTasksByUserAndStatus(long userid, string status)
        {
            User userEntity = GetEntities<User>().Where(u => u.UserID == userid).FirstOrDefault();
            if (!userEntity.UserTypeReference.IsLoaded)
            {
                userEntity.UserTypeReference.Load();
            }

            IQueryable<Task> tasks;
            if (userEntity.UserType.ID == (int)RoleType.Administrator)
            {
                tasks = GetEntities<Task>(t => t.Status.Equals(status)).OrderByDescending(t=>t.CreateDate);
            }
            else
            {
                tasks = GetEntities<Task>(t => t.User.UserID == userid && t.Status.Equals(status)).OrderByDescending(t=>t.CreateDate);
            }
            foreach (Task task in tasks)
            {
                if (!task.UserReference.IsLoaded)
                {
                    task.UserReference.Load();
                }
                if (!task.User1Reference.IsLoaded)
                {
                    task.User1Reference.Load();
                }
            }

            return tasks;
        }

        public IQueryable<Task> GetTasksByUserAndStatus(string status)
        {
            IQueryable<Task> tasks = GetEntities<Task>(t => t.Status.Equals(status)).OrderByDescending(t=>t.CreateDate);
            foreach (Task task in tasks)
            {
                if (!task.UserReference.IsLoaded)
                {
                    task.UserReference.Load();
                }
                if (!task.User1Reference.IsLoaded)
                {
                    task.User1Reference.Load();
                }
            }

            return tasks;
        }

        public IQueryable<Task> GetTasksInPogress(long userid)
        {
            User userEntity = GetEntities<User>().Where(u => u.UserID == userid).FirstOrDefault();
            if (!userEntity.UserTypeReference.IsLoaded)
            {
                userEntity.UserTypeReference.Load();
            }

            IQueryable<Task> tasks;
            if (userEntity.UserType.ID == (int)RoleType.Administrator)
            {
                tasks = GetEntities<Task>(t => !t.Status.Equals("已处理")).OrderByDescending(t => t.CreateDate);
            }
            else
            {
                tasks = GetEntities<Task>(t => t.User.UserID == userid && !t.Status.Equals("已处理")).OrderByDescending(t => t.CreateDate);
            }
            foreach (Task task in tasks)
            {
                if (!task.UserReference.IsLoaded)
                {
                    task.UserReference.Load();
                }
                if (!task.User1Reference.IsLoaded)
                {
                    task.User1Reference.Load();
                }
            }

            return tasks;
        }

        public void DealWithTask(int taskId, string result, string completedate)
        {
            Task task = GetTaskByTaskID(taskId);
            task.Result = result;
            task.CompleteDate = Convert.ToDateTime(completedate);
            task.Status = "已处理";
            Update(task);
        }

        public void Update(Task task)
        {
            UpdateEntity(task);
        }

        public Task GetTaskByTaskID(int taskID)
        {
            Task task = GetEntities<Task>(t => t.ID == taskID).FirstOrDefault();
            if (!task.UserReference.IsLoaded)
            {
                task.UserReference.Load();
            }
            if (!task.User1Reference.IsLoaded)
            {
                task.User1Reference.Load();
            }
            return task;
        }

        public IQueryable<Task> GetAllTask()
        {
            return GetEntities<Task>();
        }

        public IQueryable<Task> GetAllFinishedTask()
        {
            return GetEntities<Task>(t => t.CompleteDate.HasValue);
        }

        public IQueryable<Task> GetLatestTasks(User currentUser)
        {
            IQueryable<Task> tasks;
            if (currentUser.UserType.ID == (int)RoleType.Administrator)
            {
                tasks = GetEntities<Task>().OrderByDescending(t => t.CreateDate).Take(10);
            }
            else
            {
                tasks = GetEntities<Task>(t => t.User.UserID == currentUser.UserID).OrderByDescending(t=>t.CreateDate).Take(10);
            }
            foreach (Task task in tasks)
            {
                if (!task.UserReference.IsLoaded)
                {
                    task.UserReference.Load();
                }
            }
            return tasks;
        }

        public IQueryable<TaskFile> GetTaskFile(long taskID)
        {
            return GetEntities<TaskFile>().Where(t => t.Task.ID == taskID);
        }

        public long AddTaskFile(string fileName, long taskID)
        {
            TaskFile taskFile = new TaskFile();
            taskFile.FileName = fileName;
            taskFile.CreateDate = DateTime.Now;
            taskFile.Task = GetEntities<Task>().Where(t => t.ID == taskID).FirstOrDefault();
            InsertEntity(taskFile);
            return taskFile.ID;
        }
    }
}
